package cn.wxq.daily.empdailymgforserve.controller;

import cn.wxq.daily.empdailymgforserve.domain.Messages;
import cn.wxq.daily.empdailymgforserve.model.Result;
import cn.wxq.daily.empdailymgforserve.model.StatusCode;
import cn.wxq.daily.empdailymgforserve.service.MessageService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
@CrossOrigin //跨越处理
@RestController
@RequestMapping("/messages")
@Api(value = "MessagesController|消息公告控制器")
public class MessagesController {
    @Autowired
    private MessageService messageService;

    @Autowired private HttpServletRequest request;

    /**
     * 查询所有消息（公告）信息
     *
     * @return
     */
    @RequestMapping(value = "/findAllMessages", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询所有消息（公告）信息")
    public Result findAllMessages() {
        List<Messages> allMessages = messageService.findAllMessages();
        if (allMessages != null) {
            return new Result(true, StatusCode.OK, "获取信息成功", allMessages);
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }


    /**
     * 根据标题或类型查询数据
     *
     * @param noticeType
     * @param noticeTitle
     * @return
     */
    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    @ApiOperation(value = "根据类型或者标题查询")
    public Result findSomeMessages(@RequestParam(value = "noticeType", required = false) String noticeType, @RequestParam(value = "noticeTitle", required = false) String noticeTitle) {
        List<Messages> someMessages = messageService.findSomeMessages(noticeType, noticeTitle);
        if (someMessages != null) {
            return new Result(true, StatusCode.OK, "获取信息成功", someMessages);
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }

    @RequestMapping(value = "/addMessage", method = RequestMethod.POST)
    @ApiOperation(value = "新增消息")
    public Result addMessage(Messages messages) {
        int row = messageService.insertMessage(messages);
        if (row > 0) {
            return new Result(true, StatusCode.OK, "新增成功");
        } else {
            return new Result(false, StatusCode.ERROR, "新增失败");
        }
    }

    @RequestMapping(value = "/updateMessage", method = RequestMethod.POST)
    @ApiOperation(value = "修改消息")
    public Result updateMessage(Messages messages) {
        int row = messageService.updateMessage(messages);
        if (row > 0) {
            return new Result(true, StatusCode.OK, "修改成功");
        } else {
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "删除消息", notes = "删除消息")
    public Result deleteById(Integer mesId) {
        Claims claims = (Claims) request.getAttribute("admin_claims");
        if (claims == null) {
            return new Result(true, StatusCode.ACCESSERROR, "无权删除");
        }

        int row = messageService.deleteMessageById(mesId);

        if (row >= 0) {
            return new Result(true, StatusCode.OK, "删除消息成功");
        } else {
            return new Result(false, StatusCode.ERROR, "删除失败");
        }
    }


    /**
     * 获取详情信息
     *
     * @param mesId
     * @return
     */
    @RequestMapping(value = "/getMessageInfoById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取详情信息", notes = "获取详情信息")
    public Result getMessageInfo(Integer mesId) {
        Messages messages = messageService.selectMessageById(mesId);
        if (messages != null) {
            return new Result(true, StatusCode.OK, "获取详情信息成功", messages);
        } else {
            return new Result(true, StatusCode.ERROR, "获取详情信息失败");
        }

    }
}
